package cn.glenn.egarticle0519.base.exception;

import cn.glenn.egarticle0519.base.response.JsonResult;
import cn.glenn.egarticle0519.base.response.StatusCode;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler{
    @ExceptionHandler
    public JsonResult doHandlerIllegalArgumentException(IllegalArgumentException exception){
        log.debug("IllegalArgumentException:{}",exception.getMessage());
        return new JsonResult(StatusCode.VALIDATED_ERROR, exception.getMessage());
    }

    @ExceptionHandler
    public JsonResult doHandlerRuntimeException(RuntimeException exception){
        log.debug("RuntimeException:{}",exception.getMessage());
        return new JsonResult(StatusCode.VALIDATED_ERROR, exception.getMessage());
    }

    @ExceptionHandler
    public JsonResult doHandlerConstraintViolationException(ConstraintViolationException exception){
        log.error("ConstraintViolationException:{}",exception.getMessage());
        return new JsonResult(StatusCode.VALIDATED_ERROR, exception.getMessage());
    }

/*    @ExceptionHandler
    public JsonResult doHandleNullPointerException(NullPointerException exception){
        log.error("NullPointerException:{}",exception.getMessage());
        return JsonResult.notLogin();
    }*/

    @ExceptionHandler
    public JsonResult doHandlerMethodArgumentValidException(MethodArgumentNotValidException methodArgumentNotValidException){
        log.debug("MethodArgumentNotValidException:{}",methodArgumentNotValidException.getMessage());
        return new JsonResult(StatusCode.VALIDATED_ERROR,methodArgumentNotValidException.getFieldError().getDefaultMessage());
    }
}